package com.zkingsoft.actions.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.model.Agent;
import com.zkingsoft.model.Dictionary;
import com.zkingsoft.model.ExtensionDetail;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.RechargeDetail;
import com.zkingsoft.model.Role;
import com.zkingsoft.model.RolePwoer;
import com.zkingsoft.model.TradeRecord;
import com.zkingsoft.model.TransferRecord;
import com.zkingsoft.plugin.message.StringUtil;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.AgentService;
import com.zkingsoft.services.DictionaryService;
import com.zkingsoft.services.ExtensionDetailService;
import com.zkingsoft.services.RechargeDetailService;
import com.zkingsoft.services.RolePwoerService;
import com.zkingsoft.services.RoleService;
import com.zkingsoft.services.TransferRecordService;

/**
 * @description 后台管理action
 * @author xieguangya
 * @email 935090232@qq.com
 * @date 2016-07-13
 */
@Controller
@RequestMapping(value = "/admin/recharge")
public class BackDataController extends BaseController {
	private static final Logger log = Logger.getLogger(BackDataController.class);

	@Resource
	private RechargeDetailService currentService;
	@Resource
	private ExtensionDetailService extensionService;
	@Resource
	private AgentService agentService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private TransferRecordService transferRecordService;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(RechargeDetail detail, PaginationVO pageVo) {
		log.info("充值列表...");
		return showList(currentService, detail, pageVo);
	}
	
	@RequestMapping(value = "/showAgentList")
	public @ResponseBody AjaxResult showAgentList(Agent agent, PaginationVO pageVo) {
		log.info("代理开关页面...");
		return showList(agentService, agent, pageVo);
	}
	
	@RequestMapping(value = "/showTransferList")
	public @ResponseBody AjaxResult showTransferList(TransferRecord transfer, PaginationVO pageVo) {
		log.info("转账记录页面...");
		return showList(transferRecordService, transfer, pageVo);
	}
	
	@RequestMapping(value = "/showDictionaryList")
	public @ResponseBody AjaxResult showDictionaryList(Dictionary dictionary, PaginationVO pageVo) {
		log.info("数据字典页面...");
		return showList(dictionaryService, dictionary, pageVo);
	}
	
	@RequestMapping(value = "/extensionList")
	public @ResponseBody AjaxResult extensionList(ExtensionDetail detail, PaginationVO pageVo) throws ParseException {
		log.info("推广详情列表...");
		if(StringUtils.isNotBlank(detail.getStartTime())){
			detail.setCreateTime(DateUtils.getTimes(detail.getStartTime()));
		}
		if(StringUtils.isNotBlank(detail.getOverTime())){
			detail.setEndTime(DateUtils.getTimes(detail.getOverTime()));
		}
		return showList(extensionService, detail, pageVo);
	}
	
	@RequestMapping(value = "/delExtension")
	public @ResponseBody AjaxResult delExtension(String keys) {
		return remove(extensionService, keys);
	}
	
	@RequestMapping(value = "/delDictionary")
	public @ResponseBody AjaxResult delDictionary(String keys) {
		return remove(dictionaryService, keys);
	}
	
	@RequestMapping(value = "/delTransfer")
	public @ResponseBody AjaxResult delTransfer(String keys) {
		return remove(transferRecordService, keys);
	}
	

	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(RechargeDetail detail) {
		if (detail.getId() != null) {
			return modify(currentService, detail, "充值规则");
		} else {
			return add(currentService, detail, "充值规则");
		}
	}
	
	@RequestMapping(value = "/addOrModifyDictionary")
	public @ResponseBody AjaxResult addOrModifyDictionary(Dictionary dictionary) {
		if (dictionary.getId() != null) {
			return modify(dictionaryService, dictionary, "数据字典");
		} else {
			return add(dictionaryService, dictionary, "数据字典");
		}
	}
	
	@RequestMapping(value = "/toDictionaryForm")
	public String  toDictionaryForm(Long id) {
		if(id!=null){
			Dictionary dictionary = dictionaryService.findById(id);
			getRequest().setAttribute("obj",dictionary);
		}
		return "admin/dictionary-form";
	}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	@RequestMapping(value = "/editForm")
	public String editForm(Long id,Integer type) {
		RechargeDetail detail;
		if (id != null) {
			detail = currentService.findById(id);
			getRequest().setAttribute("obj", detail);
		}
		if(type!=null&&3==type){//推广返利
			return "admin/download-back-form";
		}else if(type!=null&&2==type){//钻石编辑
			return "admin/recharge-diamond-form";
		}
		return "admin/recharge-form";//俱乐部编辑
	}
	
	@RequestMapping(value = "/editAgentStatus")
	public String editAgentStatus(Long id) {
		Agent agent;
		if (id != null) {
			agent = agentService.findById(id);
			getRequest().setAttribute("obj", agent);
		}
		return "admin/agent-form";
	}

	@RequestMapping(value = "/agentModify")
	public @ResponseBody AjaxResult agentModify(Agent agent) {
		if (agent.getId() != null) {
			return modify(agentService, agent, "代理开关");
		} else {
			return add(agentService, agent, "代理开关");
		}
	}

}